package com.jiuzhi.app.controller;


import com.jiuzhi.app.common.wxutil.TokenUtil;
import com.jiuzhi.app.common.wxutil.model.StringUtils;
import com.jiuzhi.app.domain.Community;
import com.jiuzhi.app.domain.NcpOwner;
import com.jiuzhi.app.domain.NcpOwnerReg;
import com.jiuzhi.app.domain.YxCommunity;
import com.jiuzhi.app.service.INcpOwnerRegService;
import com.jiuzhi.app.service.INcpOwnerService;
import com.jiuzhi.app.service.IYxCommunityService;
import com.jiuzhi.common.core.controller.BaseController;
import com.jiuzhi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import static com.jiuzhi.app.common.wxutil.TokenUtil.sitePath;

@Controller
@RequestMapping(value = "/record")
public class RecordController extends BaseController {


    @Autowired
    private IYxCommunityService communityService;

    @Autowired
    private INcpOwnerService ncpOwnerService;

    @Autowired
    private INcpOwnerRegService ncpOwnerRegService;


    @Autowired
    private TokenUtil tokenUtil;


    @GetMapping("/records")
    public String record(String comId, ModelMap mmap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        if (StringUtils.isEmpty(code)) {
            String redirectUrl = sitePath + request.getRequestURI();
            if (StringUtils.isNotEmpty(request.getQueryString())) {
                redirectUrl += "?" + request.getQueryString();
            }
            redirectUrl = tokenUtil.buildOauth2UrlByBase(redirectUrl, null);
            System.out.println("----redirectUrl----" + redirectUrl);
            response.sendRedirect(redirectUrl);
            logger.info("没有找到userid,重定向到： " + redirectUrl);
        }
        request.getSession().setAttribute("code", code);
        String openId = tokenUtil.getOpenIdFromRequest(request, response);
        System.out.println(openId);
        YxCommunity community = new YxCommunity();
        NcpOwner ncpOwner = ncpOwnerService.selectByOpenId(openId);
        if (ncpOwner !=null){
            mmap.put("id",ncpOwner.getId());
            mmap.put("name",ncpOwner.getName());
            mmap.put("pinCode",ncpOwner.getPinCode());
            mmap.put("phone",ncpOwner.getPhone());
            mmap.put("build",ncpOwner.getBuildCode()+"-"+ncpOwner.getUnit()+"-"+ncpOwner.getRoomNo());
            mmap.put("contactNcpPatient",ncpOwner.getContactNcpPatient()==1?"是":"否");
            if(ncpOwner.getIllness()==1){
                mmap.put("illness",ncpOwner.getSymptom());
            }else{
                mmap.put("illness","正常");
            }

            if(ncpOwner.getNcpAreaPass()==1){
                mmap.put("ncpAreaPass",ncpOwner.getPassNcpArea());
            }else{
                mmap.put("ncpAreaPass","未经过");
            }

            mmap.put("comName",ncpOwner.getComName());
            if (ncpOwner.getType()==1){
                mmap.put("type","业主");
            }else if (ncpOwner.getType()==2){
                mmap.put("type","家属");
            }else if (ncpOwner.getType()==3){
                mmap.put("type","租客");
            }else{
                mmap.put("type","其他");
            }
            return "saoma/dj_later";
        }else{
            if(!"".equals(comId)){
                community = communityService.selectYxCommunityById(Long.parseLong(comId));
                mmap.put("comName",community.getComName());
                mmap.put("comId",community.getComId());
                mmap.put("openId",openId);
            }
            return "saoma/saoma";
        }


    }

    @GetMapping("/noHousehold")
    public String noHousehold(){
        return "saoma/fei_xqmen";
    }

    @ResponseBody
    @PostMapping("/saveData")
    public AjaxResult saveData(@Validated NcpOwner owner){
        owner.setId(UUID.randomUUID().toString().replace("-",""));
        int count = ncpOwnerService.insertNcpOwner(owner);
        if(count>0){
            return  success(owner.getId());
        }else{
            return  error();
        }
    }

    @GetMapping("/details")
    public String getRecord(String id,ModelMap mmap){
        NcpOwner ncpOwner = ncpOwnerService.selectNcpOwnerById(id);
        mmap.put("id",id);
        mmap.put("name",ncpOwner.getName());
        mmap.put("pinCode",ncpOwner.getPinCode());
        mmap.put("phone",ncpOwner.getPhone());
        mmap.put("comName",ncpOwner.getComName());
        mmap.put("build",ncpOwner.getBuildCode()+"-"+ncpOwner.getUnit()+"-"+ncpOwner.getRoomNo());
        mmap.put("contactNcpPatient",ncpOwner.getContactNcpPatient()==1?"是":"否");
        if(ncpOwner.getIllness()==1){
            mmap.put("illness",ncpOwner.getSymptom());
        }else{
            mmap.put("illness","正常");
        }

        if(ncpOwner.getNcpAreaPass()==1){
            mmap.put("ncpAreaPass",ncpOwner.getPassNcpArea());
        }else{
            mmap.put("ncpAreaPass","未经过");
        }

        if (ncpOwner.getType()==1){
            mmap.put("type","业主");
        }else if (ncpOwner.getType()==2){
            mmap.put("type","家属");
        }else if (ncpOwner.getType()==3){
            mmap.put("type","租客");
        }else{
            mmap.put("type","其他");
        }
        return "saoma/dj_later";
    }


    @ResponseBody
    @PostMapping("/updateData")
    public AjaxResult updateData(@Validated NcpOwnerReg ownerReg){
        ownerReg.setId(UUID.randomUUID().toString().replace("-",""));
        ownerReg.setNcpOwnerId(ownerReg.getNcpOwnerId());
        ownerReg.setCreateTime(new Date());
        ownerReg.setAccessTime(new Date());
        int count = ncpOwnerRegService.insertNcpOwnerReg(ownerReg);
        if(count>0){
            return success();
        }else{
            return error();
        }
    }

}
